﻿#region using
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.Windows.Markup;
using Microsoft.Win32;
using Microsoft.Surface.Presentation;
using Microsoft.Surface.Presentation.Controls;
using Microsoft.Surface.Presentation.Input;
using System.Xml;
using System.Globalization;
using System.IO;
using System.Timers;
using System.ComponentModel;
using Ab2d.Common.ReaderWmf;
using System.Windows.Forms;
using System.Collections;
using System.Windows.Media.Animation;
#endregion

namespace MultiTouchDemonstrator
{
    class SymbolManipulation : IObjectManipulation
    {
        public void ManipulationDelta(object container, ManipulationDeltaEventArgs e)
        {

            ManipulationDelta currentDelta = e.DeltaManipulation;

            // Get the Rectangle and its RenderTransform matrix.
            Image rectToMove = e.OriginalSource as Image;
            if (rectToMove != null)
            {
                TransformGroup rectsMatrix = ((TransformGroup)rectToMove.RenderTransform);
                // Get the Rectangle's current center
                if (rectsMatrix != null)
                {
                    Point orgCenter = e.ManipulationOrigin;

                    // Find the new center.
                    Point center = rectsMatrix.Transform(orgCenter);
                    TranslateTransform translate = null;
                    TranslateTransform oldTranslate = null;

                    foreach (var transform in rectsMatrix.Children)
                    {
                        if (transform is TranslateTransform)
                        {
                            translate = new TranslateTransform(currentDelta.Translation.X + ((TranslateTransform)transform).X, currentDelta.Translation.Y + ((TranslateTransform)transform).Y);
                            oldTranslate = (TranslateTransform)transform;
                        }
                    }
                    rectsMatrix.Children.Add(translate);
                    rectsMatrix.Children.Remove(oldTranslate);

                    Rect containingRect = new Rect(((FrameworkElement)e.ManipulationContainer).RenderSize);

                    Rect shapeBounds = rectToMove.RenderTransform.TransformBounds(new Rect(rectToMove.RenderSize));

                    // Check if the rectangle is completely in the window.
                    // If it is not and intertia is occuring, stop the manipulation.

                    if (e.IsInertial && !containingRect.Contains(shapeBounds))
                    {
                        e.ReportBoundaryFeedback(e.DeltaManipulation);
                        e.Complete();
                    }


                    e.Handled = true;
                }
            }
        }
        public void ManipulationStarting(object sender, ManipulationStartingEventArgs e, IInputElement container, bool handled)
        {
            e.ManipulationContainer = container;
            Canvas map = container as Canvas;
            map.IsManipulationEnabled = false;
            e.Handled = handled;
        }
        public void ManipulationInertiaStarting(object sender, ManipulationInertiaStartingEventArgs e, double translateDeceleration, double rotateDeceleration, bool handled)
        {
            e.TranslationBehavior.DesiredDeceleration = translateDeceleration;
            e.RotationBehavior.DesiredDeceleration = rotateDeceleration;
            e.Handled = handled;
        }
        public void ManipulationCompleted(object sender, ManipulationCompletedEventArgs e, UIElement container)
        {
            container.IsManipulationEnabled = true;
        }
    }
}
